package com.zpark.drug.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpark.drug.mapper.SupplierMapper;
import com.zpark.drug.pojo.Drug;
import com.zpark.drug.pojo.Supplier;
import com.zpark.drug.service.SupplierService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;
    @Override
    public List<Supplier> findAll() {
        return supplierMapper.selectAll();
    }

    @Override
    public Supplier findById(Integer id) {
        return supplierMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addSupplier(Supplier supplier) {
        return supplierMapper.insert(supplier);
    }

    @Override
    public int updateSupplier(Supplier supplier) {
        return supplierMapper.updateByPrimaryKey(supplier);
    }

    @Override
    public int deleteSupplierById(Integer id) {
        return supplierMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Supplier> findPage(int page, int size, String param) {
        PageHelper.startPage(page, size);
        Example example = new Example(Supplier.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(param)) {
            criteria.orLike("name", "%" + param + "%")
                    .orLike("content", "%" + param + "%");
        }
        return new PageInfo<>(supplierMapper.selectByExample(example));
    }



}
